import { createRouter, createWebHashHistory } from 'vue-router'
import { setupShare } from '@/lib/js/utils/share'
// import total from '../view/total.vue'
import { Api } from '../api'
import home from '../view/Home.vue'
import user from '../view/User.vue'
import signin from '../view/Signin.vue'
import imtimacy from '../view/Imtimacy.vue'
import keywords from '../view/Keywords.vue'
import gift from '../view/Gift.vue'
import wish from '../view/Wish.vue'
import mywish from '../view/Mywish.vue'
import otherwish from '../view/Otherwish.vue'
import rank from '../view/Rank.vue'
import Container from '../view/container.vue'
import friendwish from '../view/Friendwish.vue'
import explain from '../view/Explain.vue'
import { getShareIconUrl } from '@/lib/js/utils/share'
const homeShareIcon = getShareIconUrl(`${process.env.PUBLIC_PATH}/static/images/timetravel/share-main.png`)
const wishShareIcon = getShareIconUrl(`${process.env.PUBLIC_PATH}/static/images/timetravel/wish-share.png`)
const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home', component: home, name: 'home'
  },
  {
    path: '/user', component: user
  },
  {
    path: '/signin', component: signin
  },
  {
    path: '/imtimacy', component: imtimacy
  }, {
    path: '/sharedwish', component: mywish
  },
  {
    path: '/keywords', component: keywords
  },
  {
    path: '/gift', component: gift
  },
  {
    path: '/wish', component: wish, name: 'wish'
  },
  {
    path: '/mywish', component: mywish, name: 'mywish'
  },
  {
    path: '/otherwish', component: otherwish, name: 'otherwish'
  },
  {
    path: '/rank', component: rank, name: 'rank'
  },
  {
    path: '/main', component: Container, name: 'main'
  },
  {
    path: '/friendwish', component: friendwish, name: 'friendwish'
  },
  {
    path: '/explain', component: explain, name: 'explain'
  }
]

export const router = createRouter({
  history: createWebHashHistory(),
  routes: routes
})
const { HeyheyBridge } = window

router.afterEach(to => {
  const name = String(to.name) || ''
  // ----------------------- 返回按钮
  if (window.isHeyHeyTalk()) {
    if (['main', 'home', 'explain'].indexOf(name) >= 0) {
      HeyheyBridge.setTopBarBack({
        show: false
      })
    } else {
      HeyheyBridge.setTopBarBack({
        show: true
      })
    }
  }

  // ----------------------- 分享
  const heyId = localStorage.getItem('currentUserHeyId')
  const heyNickName = localStorage.getItem('currentUserNickName')

  if (process.env.APP_ENV === 'production' && heyNickName && heyId) {
    if (['mywish'].indexOf(name) >= 0) {
      setupShare({
        title: `${heyNickName}的愿望清单`,
        desc: '快来帮Ta点赞，有机会实现全部愿望哦～',
        link: `${window.location.origin}${window.location.pathname}#/otherwish?heyId=${heyId}`,
        image: wishShareIcon
      }, () => {
        const key = localStorage.getItem('__LOGIN_KEY__') || '';
        (Api as any).Wish.signUp({ key })
      })
    } else {
      setupShare({
        title: '我的嘿嘿时光机',
        desc: '快来看看这5年在嘿嘿经历了哪些有趣的事情',
        link: `${window.location.origin}${window.location.pathname}`,
        image: homeShareIcon
      })
    }
  }

  // ----------------------- 页面title
  if(['home', 'main'].indexOf(name) >= 0) {
    document.title = '我的嘿嘿时光机'
  } else {
    document.title = '新年愿望清单'
  }

})
